<script setup lang="ts">
import { getFormatDictMap } from '@/util/dict'
import { onMounted, ref } from 'vue'
import { Refresh, icon } from '@/util/icons'
import type { MenuHandleTable, MenuTable } from '@/types/menu'
import { getSysMenuHandles, getSysMenus } from '@/api/menu'
import type { MenuManageData } from '@/types/menu'
import { formatMenuManage } from '@/util/menu'
import { getRoleMenus, updateRoleMenus } from './api'
import { ElMessage } from 'element-plus'

const props = defineProps<{
  editId: number
}>()
// 注册关闭事件
const emit = defineEmits<{
  (event: 'cDlg'): void
}>()
// 系统菜单数据
const menuData = ref<MenuManageData[]>([])
// 按钮数据
const menuHandleData = ref<MenuHandleTable[]>([])
// 角色菜单权限
const roleMenus = ref<number[]>([])
// 角色按钮权限
const roleMenuHandles = ref<string[]>([])
/* 获取对应的字典信息 */
// 菜单表字典
const menuTableDict = ref<Map<string, string>>()
// 按钮表字典
const handleDict = ref<Map<string, string>>()
// 页面加载
const loading = ref<boolean>(false)
// 获取页面数据
function getPageData() {
  loading.value = true
  getSysMenus()
    .then((data) => {
      menuData.value = formatMenuManage(data.data)
    })
    .finally(() => {
      setTimeout(() => {
        loading.value = false
      }, 200)
    })
}
/**
 * 获取角色权限信息
 */
function getRolesData() {
  getRoleMenus(props.editId).then((data) => {
    roleMenus.value = data.data.mids
    roleMenuHandles.value = data.data.tags
  })
}
// 获取按钮数据
const handleLoading = ref<boolean>(false)
function getHandleData(mid: number) {
  handleLoading.value = true
  getSysMenuHandles(mid)
    .then((d) => {
      menuHandleData.value = d.data
    })
    .finally(() => {
      setTimeout(() => {
        handleLoading.value = false
      }, 200)
    })
}
// 初始化页面数据
onMounted(() => {
  getFormatDictMap('sys_menu_manage', 'head').then((d) => {
    menuTableDict.value = d
  })
  getFormatDictMap('sys_menu_handle', 'head').then((d) => {
    handleDict.value = d
  })
  getPageData()
  getRolesData()
})

// 刷新数据
function reData() {
  getPageData()
  getRolesData()
}
/* 选择器 */
const selectData = ref<MenuTable[]>([])
function onSelect(data: MenuTable[]) {
  selectData.value = data
}

// 更改菜单权限
function changeMenusAuth(mid: number) {
  const index = roleMenus.value.indexOf(mid)
  if (index >= 0) {
    roleMenus.value.splice(index, 1)
  } else {
    roleMenus.value.push(mid)
  }
}

// 更改按钮权限
// 选择菜单
function selectMenu(row: MenuTable) {
  getHandleData(row.mId!)
}
function changeMenuHandlesAuth(tags: string) {
  const index = roleMenuHandles.value.indexOf(tags)
  if (index >= 0) {
    roleMenuHandles.value.splice(index, 1)
  } else {
    roleMenuHandles.value.push(tags)
  }
}

//保存修改
function save() {
  updateRoleMenus(props.editId, roleMenus.value, roleMenuHandles.value)
    .then((d) => {
      ElMessage.success(d.msg)
      emit('cDlg')
    })
    .catch((d) => {
      ElMessage.error(d.msg)
    })
}
function cancel() {
  emit('cDlg')
}
</script>

<template>
  <div class="view">
    <div class="control">
      <div class="ctl-bts">
        <span class="ctl-l"> </span>
        <span class="ctl-r">
          <el-button :icon="Refresh" @click="reData" circle></el-button>
        </span>
      </div>
    </div>
    <div class="ctx">
      <div class="ctx-l" style="width: 50%">
        <el-table
          v-loading="loading"
          table-layout="auto"
          :stripe="true"
          :data="menuData"
          height="100%"
          :flexible="true"
          row-key="mId"
          @row-click="selectMenu"
          @selection-change="onSelect"
          highlight-current-row
        >
          <el-table-column width="150px" prop="name" align="center">
            <template #header>
              {{ menuTableDict?.get('name') || 'name' }}
            </template>
            <template #default="{ row }">
              <span>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="icon" align="center">
            <template #header>
              {{ menuTableDict?.get('icon') || 'icon' }}
            </template>
            <template #default="{ row }">
              <el-icon>
                <component :is="(icon as any)[row.icon]" />
              </el-icon>
            </template>
          </el-table-column>
          <el-table-column prop="path" align="center">
            <template #header>
              {{ menuTableDict?.get('path') || 'path' }}
            </template>
            <template #default="{ row }">
              <span class="highlight-info">{{ row['path'] }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center">
            <template #header>
              {{ menuTableDict?.get('auth') || 'auth' }}
            </template>
            <template #default="{ row }">
              <el-switch
                :modelValue="roleMenus.includes(row.mId)"
                @change="changeMenusAuth(row.mId)"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 菜单按钮展示 -->
      <div class="ctx-r">
        <el-table :data="menuHandleData" v-loading="handleLoading">
          <el-table-column width="200" label="按钮效果" align="center">
            <template #="{ row }">
              <el-button
                :icon="(icon as any)[row.icon]"
                :type="row.type"
                v-if="row.show == '1'"
                :disabled="row.status == '0'"
                :circle="row.style == 'circle'"
                :plain="row.style == 'plain'"
                :round="row.style == 'round'"
                >{{ row.name }}</el-button
              >
            </template>
          </el-table-column>
          <el-table-column align="center" :label="handleDict?.get('name') || 'name'" prop="name">
            <template #default="{ row }">
              <span class="edit-text">{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center">
            <template #header>
              {{ handleDict?.get('auth') || 'auth' }}
            </template>
            <template #default="{ row }">
              <el-switch
                :modelValue="roleMenuHandles.includes(row.tag)"
                @change="changeMenuHandlesAuth(row.tag)"
              />
            </template>
          </el-table-column>
          <el-table-column
            width="150"
            align="center"
            :label="handleDict?.get('tag') || 'tag'"
            prop="tag"
          >
            <template #default="{ row, $index }">
              <span class="edit-text">{{ row.tag }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="buttons">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="save">保存</el-button>
    </div>
  </div>
</template>
<style scoped lang="less">
@import '../../../assets/page_bgl.less';
.ctx {
  display: flex;
  height: 300px;
  .ctx-l,
  .ctx-r {
    width: 50%;
  }
}
.buttons {
  display: flex;
  margin-top: 30px;
  justify-content: flex-end;
}
</style>
